﻿using System.Collections;
using UnityEngine;

namespace Scripts
{
    public class Bullet : MonoBehaviour
    {
        private Transform targeMonster;//目标敌人
        [SerializeField]
        private float speed=15f;//速度

        public void SetTargetMonster(Transform targeMonster) 
        {
            this.targeMonster = targeMonster;
        }

        private void Update()
        {
            if (targeMonster)//如果目标敌人还没死就往敌人方向飞
            {
                //子弹移动
                transform.position = Vector3.MoveTowards(transform.position, targeMonster.position, Time.deltaTime * speed);
                //如果到达敌人位置
                if (transform.position == targeMonster.position)
                {
                    //让敌人受伤
                    targeMonster.GetComponent<Monster>().Damage(1);
                    //销毁子弹自己
                    Destroy(this.gameObject);
                }
            }
            else//目标敌人已经死亡,直接往前飞
            {
                transform.Translate(Vector3.forward*Time.deltaTime*speed);
                if (transform.position.y<1f)
                {
                    Destroy(this.gameObject);
                }
            }
          
        }
    }
}